package cn.hznc.configuration;


//@Configuration
//@IntegrationComponentScan
//public class MqttReceiveConfig {
//
//    @Autowired
//    private MqttService mqttService;
//
//    @Value("${spring.mqtt.username}")
//    private String username;
//
//    @Value("${spring.mqtt.password}")
//    private String password;
//
//    @Value("${spring.mqtt.url}")
//    private String hostUrl;
//
//    @Value("${spring.mqtt.client.id}")
//    private String clientId;
//
//    @Value("${spring.mqtt.default.topic}")
//    private String defaultTopic;
//
//    private MqttPahoMessageDrivenChannelAdapter adapter;
//
//
//    /**
//     * 此处可以使用其他消息通道
//     * Spring Integration默认的消息通道，它允许将消息发送给一个订阅者，然后阻碍发送直到消息被接收。
//     */
//    @Bean
//    public MessageChannel mqttInputChannel() {
//        return new DirectChannel();
//    }
//
//    @Bean
//    public MqttConnectOptions getMqttConnectOptions_in(){
//        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
//        mqttConnectOptions.setUserName(username);
//        mqttConnectOptions.setPassword(password.toCharArray());
//        mqttConnectOptions.setServerURIs(new String[]{hostUrl});
//        //心跳
//        mqttConnectOptions.setKeepAliveInterval(60);
//        //自动重新连接
//        mqttConnectOptions.setAutomaticReconnect(true);
//        return mqttConnectOptions;
//    }
//
//    @Bean
//    public MqttPahoClientFactory mqttClientFactory_in() {
//        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
//        factory.setConnectionOptions(getMqttConnectOptions_in());
//        return factory;
//    }
//
//
//    //客户端初始化，适配器
//    @Bean
//    public MessageProducer inbound() {
//        adapter =
//                new MqttPahoMessageDrivenChannelAdapter(clientId+"_inbound", mqttClientFactory_in(),
//                        "Discovery/Response","Probe/Query/Response/#","Query/Response/#",
//                        "Register/Request",
//                        "Probe/Version/#","Sample/+/sample_channel0");
//        adapter.setCompletionTimeout(10000);
//        adapter.setConverter(new DefaultPahoMessageConverter());
//        adapter.setQos(1);
//        adapter.setOutputChannel(mqttInputChannel());
//        return adapter;
//    }
//
//
//    //通过通道获取数据
//    @Bean
//    @ServiceActivator(inputChannel = "mqttInputChannel")
//    public MessageHandler handler() {
//        System.err.println("我开始执行了，接受数据");
//        return message -> {
//            try {
//                    mqttService.handle_message(((message.getHeaders().get("mqtt_receivedTopic"))).toString(),
//                        JSON.parseObject(message.getPayload().toString()));
//
//            } catch (NumberFormatException e) {
//                System.out.println(e.getMessage());
//                e.printStackTrace();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        };
//    }
//}